package com.example.bat;

import com.example.dao.BatDao;
import com.example.home.*;
import com.example.pojo.BatPojoUser;
import com.example.utils.BatUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("bat/")
public class BatController {
    SqlSession sqlSession;
    @GetMapping("index")
    public Object bat_index(){
        List<BatPojoUser> list = null;
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            list = mapper.get_data_list();
            if (sqlSession != null){
                sqlSession.close();
            }
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        return BRespond.respond_success(list);
    }
    @PostMapping(value = "search")
    public  Object bat_search(BPage bPage){
        List<BatPojoUser> list = null;
        try {
            int page_num = bPage.getPage() > 0 ? bPage.getPage() : 1;
            int size_num = bPage.getSize() > 0 ? bPage.getSize() : 20;
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            list = mapper.get_data_list_page((page_num-1)*size_num,size_num);
            if (sqlSession != null){
                sqlSession.close();
            }
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        return BRespond.respond_success(list);
    }
    @PostMapping("search_get_user")
    public Object bat_search_get_user(BatPojoUser user){
        List<BatPojoUser> list = null;
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            list = mapper.get_data_user(user);
            if (sqlSession != null){
                sqlSession.close();
            }
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        return BRespond.respond_success(list);
    }
    @PostMapping("user")
    public  Object bat_user(String user_id){
        BatPojoUser user = null;
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            user = mapper.get_data(user_id);
            BatPojoUser user1 = mapper.get_data(user_id);
            if (sqlSession != null){
                sqlSession.close();
            }
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
        return BRespond.respond_success(user);
    }
    @PostMapping("insert")
    public Object bat_insert(BatPojoUser user){
        ArrayList<BatPojoUser> list = new ArrayList<>();
        if (user!= null){
            list.add(user);
        }
        return bat_insert_datas(list);
    }
    @PostMapping("insertdatas")//大数据插入map 方式
    public Object bat_insert_datas(List<BatPojoUser> list){
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            mapper.inseart_datas(list);
            sqlSession.commit();
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping("insertdatatoall")//批处理
    public Object bat_insert_data_total(List<BatPojoUser> list){
        try {
            long start = System.currentTimeMillis();
            sqlSession = BatUtils.session(ExecutorType.BATCH,false);
            BatDao mapper = sqlSession.getMapper(BatDao.class);
//            list.stream().forEach(objc ->{
//                mapper.insert_data(objc);
//            });
            list.stream().forEach(student -> mapper.insert_data(student));
            sqlSession.commit();
            sqlSession.clearCache();
            System.out.println("共计耗时"+(System.currentTimeMillis() - start));
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping("update")
    public Object bat_update(BatPojoUser user){
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            mapper.update_data(user);
            sqlSession.commit();
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping("delete")
    public Object bat_delete(String user_id){
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            mapper.delete_data(user_id);
            sqlSession.commit();
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping("truncate")
    public  Object bat_truncate(){
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            mapper.truncate_table();
            sqlSession.commit();
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
    @PostMapping("add_column")
    public  Object bat_add_column(int add){
        try {
            sqlSession = BatUtils.session();
            BatDao mapper = sqlSession.getMapper(BatDao.class);
            if (add == 1) {
                mapper.alter_column_add_phone();
            } else {
                mapper.alter_column_drop_phone();
            }
            sqlSession.commit();
            if (sqlSession != null){
                sqlSession.close();
            }
            return BRespond.respond_success(true);
        } catch (Exception e) {
            return BRespond.respond_failure_message(205,e.getMessage());
        }
    }
}
